package com.jiusuzu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiusuzu.common.pojo.EUTreeNode;
import com.jiusuzu.common.pojo.JiusuzuResult;
import com.jiusuzu.mapper.TbContentCategoryMapper;
import com.jiusuzu.mapper.TbContentMapper;
import com.jiusuzu.pojo.TbContent;
import com.jiusuzu.pojo.TbContentCategory;
import com.jiusuzu.pojo.TbContentCategoryExample;
import com.jiusuzu.pojo.TbContentCategoryExample.Criteria;
import com.jiusuzu.service.ContentService;

/**
 * 内容管理
 * 
 * @author 李锐洋
 */
@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;
	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;

	@Override
	public JiusuzuResult insertContent(TbContent content) {
		// 补全pojo内容
		content.setCreated(new Date());
		content.setUpdated(new Date());
		contentMapper.insert(content);
		return JiusuzuResult.ok();
	}

	@Override
	public List<EUTreeNode> getCategoryList(long parentId) {
		// 根据parentId查询节点列表
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		List<EUTreeNode> resultList = new ArrayList<>();
		for (TbContentCategory tbContentCategory : list) {
			// 创建一个节点
			EUTreeNode node = new EUTreeNode();
			node.setId(tbContentCategory.getId());
			node.setText(tbContentCategory.getName());
			node.setState(tbContentCategory.getIsParent() ? "closed" : "open");
			resultList.add(node);
		}
		return resultList;
	}
}